import React from "react";
import ReactDOM from "react-dom";
import {HashRouter as Router, Route, Link, Switch, NavLink, Redirect } from "react-router-dom"
import {createStore} from "redux";
import {Provider,connect} from "react-redux"
import Goods from "./components/goods.jsx"
import Cart from "./components/Cart.jsx"
import testcss from "./css/test.scss"
import store from "./redux/reducer.jsx"
import {mapStateToProps,mapDispatchToProps} from "./redux/action.js"


class App extends React.Component{
	constructor(){
		super()
		this.state={
			list:[]
		}
	}
	componentWillMount(){
		store.subscribe(()=>{
			this.setState({
				list:this.props.cart
			})
		})
	}
		//统计数量
	count(){
		console.log(this.props.cart)
		var num=0;
		for(var i in this.props.cart){
			num+=this.props.cart[i].num
		}
		return num;
		console.log("aa")
	}
	render(){
		return(
			<div className="box">
				<div className="header">
					<div>
						<NavLink to="/goods">功法</NavLink>						
					</div>
					<div className="cart">
						<NavLink to="/cart">购物车</NavLink>
						<span className="xiao">{this.count()}</span>
					</div>
				</div>
				<div className="show">
					<Switch>
			          <Route path="/goods" component={Goods}/>
			          <Route path="/cart" component={Cart}/>
					</Switch>
				</div>
			</div>
		)
	}
}
App=connect(mapStateToProps,mapDispatchToProps)(App)
ReactDOM.render(
	<Provider store={store}>
		<Router>
			<Switch>
				<Route path="/" component={App}/>
			</Switch>
		</Router>
	</Provider>
	, document.getElementById("app"));
